package com.medium.house.service.impl;

import com.medium.house.config.ConfigBean;
import com.medium.house.entity.WechatUser;
import com.medium.house.request.WechatUserRequestDto;
import com.medium.house.service.WechatAuthService;
import com.medium.house.service.WechatUserService;
import com.medium.house.utils.JacksonUtils;
import com.medium.house.utils.http.HttpRequest;
import com.medium.house.vo.WechatSignVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author : wang.yu
 * @Description : 获取微信小程序 (openid/appid/... 等微信相关信息)
 * @Date: Created in 2018/1/30 0030  11:14
 * @Modified By
 */
@Service
@Slf4j
public class WechatAuthServiceImpl implements WechatAuthService {
    @Autowired
    private ConfigBean configBean;

    @Autowired
    private WechatUserService wechatUserService;

    @Override
    public WechatSignVO getWechatAuthSign(String code) {
        WechatSignVO wechatSignVO = new WechatSignVO();
        log.info("WechatAuthServiceImpl getWechatAuthSign transmap:{} code:{}",code,code);
        //获取交易返回结果
        String rquUrl = configBean.getUrl().replace("JSCODE",code);
        String getReturnString = HttpRequest.get(rquUrl);
        log.info("WechatAuthServiceImpl getWechatAuthSign getReturnString:{} code:{}",getReturnString,code);
        Map<String, String> map =  JacksonUtils.extractMap(getReturnString,new HashMap<String, String>());
        //错误时返回JSON数据包(示例为Code无效)
        if (!StringUtils.isEmpty(map.get("errcode"))){
            return null;
        }
        //正常返回的JSON数据包
        wechatSignVO.setOpenid(map.get("openid"));
        //wechatSignVO.setSessionKey(map.get("session_key"));
        //wechatSignVO.setUnionid(map.get("unionid"));
        WechatUser wechatUser = new WechatUser();
        BeanUtils.copyProperties(wechatSignVO,wechatUser);
        if (wechatUserService.add(wechatUser) == 0){
            log.info("WechatAuthServiceImpl getWechatAuthSign wechatUserService.add:{} ",wechatUser);
            return null;
        }
        return wechatSignVO;
    }


    /**
     * 获取 openid/session_key
     * @param wechatUserRequestDto
     * @return
     */
    @Override
    public WechatSignVO getWechatAuthSign(WechatUserRequestDto wechatUserRequestDto){
        log.info("WechatAuthServiceImpl getWechatAuthSign({})",wechatUserRequestDto);
        ///获取交易返回结果
        String rquUrl = configBean.getUrl().replace("JSCODE",wechatUserRequestDto.getCode());
        String getReturnString = HttpRequest.get(rquUrl);
        log.info("WechatAuthServiceImpl getReturnString : {}",getReturnString);
        Map<String, String> map =  JacksonUtils.extractMap(getReturnString,new HashMap<String, String>());
        //错误时返回JSON数据包(示例为Code无效)
        if (!StringUtils.isEmpty(map.get("errcode"))){
            return null;
        }
        /*map.put("openid","1");
        map.put("session_key","1");

        map.put("unionid","1");*/
        String openid = map.get("openid");
        WechatUser wechatUser = wechatUserService.getWechatUserByOpenid(openid);
        if (null == wechatUser){
            //正常返回的JSON数据包
            wechatUser = new WechatUser().initialize();
            BeanUtils.copyProperties(wechatUserRequestDto,wechatUser);
            wechatUser.setOpenid(openid);
            wechatUser.setSessionKey(map.get("session_key"));
            wechatUser.setSessionKey(map.get("unionid"));
            if (wechatUserService.add(wechatUser) == 0){
                log.info("WechatAuthServiceImpl getWechatAuthSign wechatUserService.add:{} ",wechatUser);
                return null;
            }
        }
        WechatSignVO wechatSignVO = new WechatSignVO();
        wechatSignVO.setOpenid(wechatUser.getOpenid());
        return wechatSignVO;
    }
}
